import { Application } from '../Application';
import { LogLevel, miscCfg } from './defines';

/**
 * 日志输出
 */
export class Logger {

    app: Application;

    init(app: Application) {
        this.app = app;
        return this;
    }

    log(level: LogLevel, msg: any, ...srcs: any[]) {
        let prefix = `[${this.app.svr.id}][${level.toUpperCase()}]`;
        if (srcs && srcs.length) {
            prefix += `[${srcs.map(src => typeof src === "function" ? src.name : typeof src === "string" ? src : src.constructor.name).join(".")}]`
        }
        miscCfg.logger && miscCfg.logger(level, prefix + " -> " + msg);
    }

    debug(msg: any, ...source: any[]) {
        this.log(LogLevel.debug, msg, ...source);
    }

    info(msg: any, ...source: any[]) {
        this.log(LogLevel.info, msg, ...source);
    }

    warn(msg: any, ...source: any[]) {
        this.log(LogLevel.warn, msg, ...source);
    }

    error(msg: any, ...source: any[]) {
        this.log(LogLevel.error, msg, ...source);
    }

}

/** 日志输出 */
export const logger = new Logger();